package com.qf.smsplatform.strategy.service.impl;

import com.qf.smsplatform.common.constants.CacheConstants;
import com.qf.smsplatform.common.constants.StrategyConstants;
import com.qf.smsplatform.common.index.StandardSubmit;
import com.qf.smsplatform.strategy.api.CacheService;
import com.qf.smsplatform.strategy.service.FilterChain;
import com.qf.smsplatform.strategy.util.PushMessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service(value = "black")
@Slf4j
public class BlackStrategy implements FilterChain {

    @Resource
    private CacheService cacheService;

    @Resource
    private PushMessageUtil pushMessage;
    @Override
    public boolean strategy(StandardSubmit submit) {
        log.info("【黑名单策略】  黑名单策略执行啦");

        //1.submit中获取发送的手机号码

        String mobile = submit.getDestMobile();


        //2.通过cacheService，将手机号key去redis查询
        String result = cacheService.get(CacheConstants.CACHE_PREFIX_BLACK + mobile);


        //3.返回1，证明手机号是黑名单---如果是黑名单，需要发送什么消息，返回false
        if (StringUtils.isEmpty(result) || !result.equalsIgnoreCase("1")){
            //3.正常返回true，记录日志，放行即可
            log.info("【黑名单策略校验】 校验通过啦");
            return  true;

        }else {

            //4.返回1，证明手机号是黑名单
            log.info("【黑名单策略校验】 校验失败");

            //4.1 发送消息，Submit，作为记录日志的信息
            pushMessage.pushLog(submit, StrategyConstants.STRATEGY_ERROR_BLACK);



            //4.2发送消息，report，作为借口模块接收的消息，借口模块给客户回调
            pushMessage.pushReport(submit, StrategyConstants.STRATEGY_ERROR_BLACK);


        //黑名单未通过，返回false
            return  false;

        }




    }
}
